(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.transaction.amendment.listController', listController);
	listController.$inject = [ '$scope', '$q', '$filter', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService',
			'transactionService' ];
	function listController($scope, $q, $filter, Constants, gridService, modalService, layoutConfig, validatorConfig, authService, transactionService) {
		var ctrl = this, txnAmendmentInfo = $scope.$stateParams.params; 
		var log = $scope.loggerFactory.getLogger("amendmentList");
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.init = function() {
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			transactionService.dataFilter().then(function(response){
				ctrl.transactionStatusFilter = response.transactionStatusFilter;
				ctrl.transactionActionFilter = response.transactionActionFilter;
				ctrl.transactionPayFilter = response.transactionPayFilter;
				transactionService.getTxnStatus().then(function(result){
	            	ctrl.txnStatus = result.txnStatus;
	            	ctrl.payStatus = result.payStatus;
	            	ctrl.txnCcys = result.txnCcys;
	            	ctrl.search();
				});
			});
		};

		ctrl.view = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('cda.transaction.amendment.view', ctrl.selectedRows[0]);
		};
		
		ctrl.search = function() {
			ctrl.searchCriteria.txnId = $filter('encode')(txnAmendmentInfo.txnId);
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(
			function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			},angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);